﻿
@inject IHttpService _httpService

@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IPopupService PopupService


@if (string.IsNullOrWhiteSpace(Model.Name) == false)
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@("编辑 " + Model.Name)"/>
}
else
{
    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip/>

}
<AutoSaveTip TItem="EditLotteryModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />
<div>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {

        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="@($"管理 {Model.Name}（Id:{Model.Id}）")" Icon="@IconType.Lottery.ToIconString()" Outline>
         <MDCard Class="mb-4">
             <MTabs @bind-Value="TabIndex" IconsAndText Grow CenterActive>

                 <MTab Value="1">
                     编辑<MIcon>mdi-pencil</MIcon>
                 </MTab>
                 <MTab Value="2">
                     参与<MIcon>mdi-account-multiple </MIcon>
                 </MTab>
                 <MTab Value="3">
                     中奖<MIcon>mdi-gift</MIcon>
                 </MTab>
             </MTabs>
            </MDCard>

         <MTabsItems @bind-Value="TabIndex">
             <MTabItem Value="1">
                 <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Lotteries.MainCard Model="Model" EntryGameItems="EntryGameItems" Class="mb-4" />
                 <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Lotteries.MainPageCard Model="Model" Class="mb-4" />
                 <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Lotteries.AwardsCard Model="Model" Class="mb-4" />
                 <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />
             </MTabItem>
             <MTabItem Value="2">
                 <CnGalWebSite.Shared.Component.Lotteries.EditPage.ListLotteryUserTip Id="Model.Id" />
             </MTabItem>
             <MTabItem Value="3">
                 <CnGalWebSite.Shared.Component.Lotteries.EditPage.ListWinnerCard Id="Model.Id" />
             </MTabItem>
         </MTabsItems>
        </CnGalWebSite.Components.Containers.MasaTitleContainer>
            
    }
</div>
@code {
    [Parameter]
    public string Id { get; set; }

    private EditLotteryModel Model { get; set; } = new EditLotteryModel();

    private bool isReady = false;
    public StringNumber TabIndex { get; set; } = 1;
    private List<string> EntryGameItems { get; set; }


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    AutoSaveTip<EditLotteryModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                Model = await _httpService.GetAsync<EditLotteryModel>("api/lotteries/EditLottery/" + Id);
                EntryGameItems = await _httpService.GetAsync<List<string>>("api/entries/GetAllEntries/0");

                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取所有词条名称列表失败");
            }
        }
    }
    private async Task OnSubmit()
    {
        try
        {
            var obj = await _httpService.PostAsync<EditLotteryModel, Result>("api/lotteries/EditLottery", Model);
            //判断结果
            if (obj.Successful == false)
            {

                await PopupService.ToastErrorAsync("编辑抽奖失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync("编辑抽奖成功", "编辑抽奖成功");
                NavigationManager.NavigateTo("/lotteries/index/" + Model.Id);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "编辑抽奖失败");
        }
    }

    private void DataLoaded(EditLotteryModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();

    }

}
